{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "html_str = \"\"\"\n",
    "<body>\n",
    "<div class=\"container\">\n",
    "    <div id=\"first\">\n",
    "        <div class=\"one\">都市</div>\n",
    "        <div class=\"two\">德玛西亚</div>\n",
    "        <div class=\"two\">王牌对王牌</div>\n",
    "        <a>\n",
    "            <div class=\"spe\">特殊位置</div>\n",
    "        </a>\n",
    "    </div>\n",
    "    <div id=\"second\">\n",
    "        <div class=\"three four\">水电费</div>\n",
    "        <div class=\"three\">说的话房间不开封</div>\n",
    "        <div class=\"four\">三顿饭黑客技术</div>\n",
    "    </div>\n",
    "    <div id=\"third\">\n",
    "        <div class=\" three\">水电费</div>\n",
    "        <div class=\"three\">说的话房间开封</div>\n",
    "    </div>\n",
    "</div>\n",
    "</body>\n",
    "\"\"\"\n",
    "\n",
    "html = etree.HTML(html_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['水电费', '说的话房间不开封', '水电费', '说的话房间开封']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@class=\"three\"]/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['说的话房间不开封', '说的话房间开封']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@class=\"three\"]/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['水电费', '说的话房间不开封', '水电费', '说的话房间开封']\n"
     ]
    }
   ],
   "source": [
    "## contains\n",
    "print(html.xpath('.//div[contains(@class, \"three\")]/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['三顿饭黑客技术']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[@class=\"four\"]/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['水电费', '三顿饭黑客技术']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"four\")]/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n        ', '水电费', '\\n        ', '说的话房间不开封', '\\n        ', '三顿饭黑客技术', '\\n    ']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@id, \"o\")]//text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['水电费', '水电费']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(text(), \"水电\")]/text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
